package ferp.android.log;

import ferp.android.BuildConfig;
import ferp.core.game.Settings;
import ferp.core.log.Log;

import java.io.*;
import java.sql.Timestamp;

/**
 * User: olegs
 * Date: 7/4/12 10:59 AM
 */
public class Alog extends Log
{
  @Override
  protected void d(String tag, String msg)
  {
    if (BuildConfig.DEBUG)
      android.util.Log.d(tag, msg);
  }

  @Override
  protected void e(String tag, Throwable t)
  {
    if (BuildConfig.DEBUG)
    {
      android.util.Log.e(tag, "Caught exception", t);

      try
      {
        File f = File.createTempFile("err", ".txt", Settings.Folders.instance(null).logs);
        BufferedWriter osw = new BufferedWriter(new FileWriter(f.getPath(), false));

        try
        {
          StringWriter sw = new StringWriter();
          PrintWriter pw = new PrintWriter(sw);
          pw.append(new Timestamp(System.currentTimeMillis()).toString()).append('\n');
          t.printStackTrace(pw);

          osw.write(sw.toString());
        }
        finally
        {
          osw.close();
        }
      }
      catch (Exception e)
      {
        // nothing to do
      }
    }
  }

  private static final UncaughtExceptionHandler ueh = new UncaughtExceptionHandler();
}
